<?php

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();
$return['account']=$_SESSION['account'];
$return['extradata']=array();
$setting=uni_setting('register');
$setting['login_type']=intval($setting['login_type']);
if($_RQ['op']=='get'){
	$return['item']=array();
	$return['title']='登录';
	$item=array();
	if(!$setting['login_type']){
		$return['show_choose']=true;
		$item['login_type']=1;
	}else{
		$item['login_type']=$setting['login_type'];
	}
	if($_RQ['forward']){
		$item['forward']=$_RQ['forward'];
	}
	$return['item']=$item;
	
	exi($return);
}
if($_RQ['op']=='login'){
	//登陆
	$post=get_postdata();
	
	if(!$setting['login_type']){
		//如果设置为二者皆可
		$setting['login_type']=intval($post['login_type']);
	}
	if($setting['login_type']==2){
		//手机验证码登陆
		if(!check_mobile_code($post['smscode'])){
			exi('验证码错误！','error');
		}
		$uid=pdo_getcolumn('core_members',array('mobile'=>trim($post['mobile']),'uniacid'=>$_SESSION['uniacid']),'id');
		if(!$uid){
			exi('用户不存在，请先注册!','error');
		}
	}elseif($setting['login_type']==1){
		//账号密码登陆
		if(!$post['username']){
			exi('请输入账号！','error');
		}
		if(!$post['password']){
			exi('请输入密码！','error');
		}
		$user=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'username'=>$post['username']),array('id','salt','password'));
		if(empty($user)){
			exi('用户不存在，请先注册!','error');
		}
		if(!check_password($post['password'],$user['salt'],$user['password'])){
			exi('账号或密码错误!','error');
		}
		$uid=$user['id'];
	}else{
		exi('登陆方式错误！','error');
	}
	$_SESSION['uid']=$uid;
	$_SESSION['member']=pdo_get('core_members',array('id'=>$uid,'uniacid'=>$_SESSION['uniacid']),array('username','credit1','credit2','nickname','avatar'));
	if($_SESSION['openid']){
		pdo_update('core_fans',array('mid'=>$uid),array('openid'=>$_SESSION['openid']));
	}
	$_SESSION['agent']=$this->get('member',array('mid'=>$_SESSION['uid']));
	exi('登陆成功！','success',custom_url('jy_move_agentcar/member.index/',true));
}
if($_RQ['op']=='api'){
	$params=array(
		'mid'=>$_RQ['mid'],
		'timestamp'=>$_RQ['timestamp'],
		'nonce'=>$_RQ['nonce'],
	);
	if($params['timestamp']<TIMESTAMP-300 || $params['timestamp']>TIMESTAMP+3000){
		exi('登陆失败！时间戳校验失败！','error');
	}
	if($_RQ['sign']!=get_sign($params)){
		exi('登陆失败！签名校验失败！','error');
	}
	$uid=intval($params['mid']);
	$_SESSION['uid']=$uid;
	$_SESSION['member']=pdo_get('core_members',array('id'=>$uid,'uniacid'=>$_SESSION['uniacid']),array('username','credit1','credit2','nickname','avatar'));
	$_SESSION['agent']=$this->get('member',array('mid'=>$_SESSION['uid']));
	exi('登陆成功！','success',custom_url('jy_move_agentcar/member.index/',true));
}
?>